package com.tenghu.swa.cus.controller;

import com.tenghu.swa.cus.entity.Customer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Arvin on 2017/12/7.
 */

@RestController
@RequestMapping(value = "/cus")
@Api(value = "客户服务接口",description = "提供对客户操作服务接口（增、删、改、查）用户信息")
public class CusController {
    private static List<Customer> customerList=new ArrayList<Customer>();

    static {
        customerList.add(new Customer("1","G00001","广州视源股份有限公司","CVTE","广州市黄埔区云埔6号"));
        customerList.add(new Customer("2","G00002","广州赛意信息科技股份有限公司","SIE","佛山市顺德区北滘镇新城区怡和路怡和中心11楼"));
    }

    @RequestMapping(value = "/all",method = RequestMethod.GET)
    @ApiOperation(value = "获取所有客户信息",notes = "获取当前所有有效客户信息")
    public List<Customer> getAllCustomer(){
       return customerList;
    }

    @RequestMapping(value = "/get/{cusId}",method = RequestMethod.GET)
    @ApiOperation(value = "根据客户ID获取客户信息",notes = "根据客户ID获取客户信息")
    @ApiImplicitParam(paramType = "path",name = "cusId",required = true,value = "客户ID",dataType = "string")
    public Customer getCusById(@PathVariable("cusId") String cusId){
        for (Customer customer:customerList){
            if(cusId.equals(customer.getCusId())){
                return customer;
            }
        }
        return null;
    }


    @RequestMapping(value = "/save",method = RequestMethod.POST)
    @ApiOperation(value = "保存客户信息",notes = "保存客户信息",httpMethod = "POST")
    public List<Customer> saveCus(
            @ApiParam(name = "customer",required = true,type = "object",value = "客户信息")
                    Customer customer){
        customerList.add(customer);
        return customerList;
    }

    @RequestMapping(value = "/del/{cusId}",method = RequestMethod.GET)
    @ApiOperation(value = "刪除客戶信息",notes = "根据客户ID删除客户信息")
    @ApiImplicitParam(value = "客户ID",name = "cusId",paramType = "path",required = true)
    public List<Customer>  deleteCus(@PathVariable("cusId") String cusId){
                for (Customer customer:customerList){
                    if (cusId.equals(customer.getCusId())){
                        customerList.remove(customer);
                    }
                }
        return customerList;
    }
}
